home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 3dkit1.zip / OFFTO3DV.C < prev    next >
Text File  |  1992-05-26  |  2KB  |  89 lines

  1. /* OFFto3DV.c  -  convert OFF format to 3DV */
  2.  
  3. /* Oscar Garcia <garciao@mof.govt.nz>, May 1992 */
  4.  
  5. /* uses getopt */
  6. int getopt(int argc, char *argv[], char *options);
  7. extern  int optind;
  8. extern char *optarg;
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12.  
  13. #define USAGE "usage: OFFto3DV [/cn] [infile] [outfile]\n\
  14. \tIf file names are omitted, the standard i/o streams are used.\n\
  15. \tOptions (with defaults):\n\
  16. \t\t/c9  -  colour\n"
  17.  
  18. #define ERROR(msg) {fputs(msg,stderr),exit(1);}
  19. #define PERROR(msg) {perror(msg),exit(1);}
  20.  
  21. void main(int argc, char* argv[])
  22. {
  23.     int npoints, npolygons, nedges, i, n, p, q, colour = 9;
  24.     FILE *infile = stdin, *outfile = stdout;
  25.     char line[82];
  26.     char options[] = "c:";
  27.  
  28.     /* get options */
  29.     while ((n = getopt(argc, argv, options)) != EOF)
  30.     {    if (n == '?')
  31.             ERROR(USAGE)
  32.         else
  33.             colour = atoi(optarg);
  34.     }
  35.  
  36.     /* open files */
  37.     if (optind < argc - 2)
  38.         ERROR(USAGE);    /* too many */
  39.     if (optind < argc)
  40.     {    infile = fopen(argv[optind], "rt");
  41.         if (infile == NULL)
  42.             ERROR("Can't open input file");
  43.         if (++optind < argc)
  44.         {    outfile = fopen(argv[optind], "wt");
  45.             if (outfile == NULL)
  46.                 ERROR("Can't open output file");
  47.         }
  48.     }
  49.  
  50.     /* first line (counts) */
  51.     if (3 != fscanf(infile, "%d %d %d", &npoints, &npolygons, &nedges) ||
  52.             NULL == fgets(line, 81, infile))
  53.         ERROR("Error reading first line");
  54.     fprintf(outfile, "%d\n", npoints);
  55.  
  56.     /* copy coordinates */
  57.     for (i = 1; i <= npoints; i++)
  58.     {    if (NULL == fgets(line, 81, infile))
  59.         {    fprintf(stderr, "Error reading point #%d", i);
  60.             exit(1);
  61.         }
  62.         fputs(line, outfile);
  63.     }
  64.  
  65.     /* process polygons */
  66.     fprintf(outfile, "%d\n", npolygons + nedges);
  67.     for (i = 1; i <= npolygons; i++)
  68.     {    if (2 != fscanf(infile, "%d %d", &n, &p))
  69.             ERROR("Bad input: polygon not found");
  70.         nedges -= n;    /* for checking */
  71.         fprintf(outfile, "%d 0\n", p);     /* move to polygon start */
  72.         while (--n > 0)
  73.         {    fscanf(infile, "%d", &q);
  74.             fprintf(outfile, "%d %d\n", q, colour); /* draw */
  75.         }
  76.         fprintf(outfile, "%d %d\n", p, colour); /* close the polygon */
  77.     }
  78.     if (ferror(infile))
  79.         PERROR("Error reading file");
  80.     if (feof(infile))
  81.         ERROR("Unexpected end of file on input");
  82.     fclose(infile);
  83.     if (ferror(outfile))
  84.         PERROR("Error writing output");
  85.     fclose(outfile);
  86.     if (nedges != 0)
  87.         ERROR("Number of edges does not match header");
  88. }
  89.